package com.bone.modules.message.controller;

import com.bone.common.vo.Result;
import com.bone.modules.message.event.NoticeEvent;
import com.bone.modules.message.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author :
 * @date : 2020/12/29 18:12
 * Description :
 */
@Api(tags = "异步事件")
@RestController
@RequestMapping("/event")
@Slf4j
public class EventController {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    private IMessageService messageService;

    @ApiOperation("事件测试")
    @GetMapping("/notice/{message}")
    public Result<String> notice(@PathVariable(name = "message") String message) {
        log.info("begin >>>>>>");
        /* applicationEventPublisher.publishEvent(new NoticeEvent(message));*/
        Future<String> future = messageService.sendMessage(message);
        log.info("end <<<<<<");
        while (true) {
            if (future.isCancelled()) {
                log.info("deal async task is Cancelled");
                break;
            }
            if (future.isDone()) {
                log.info("deal async task is Done");
                try {
                    log.info("return result is {}", future.get());
                } catch (InterruptedException e) {
                    log.info("InterruptedException异常显示内容：{}", e);
                } catch (ExecutionException e) {
                    log.info("ExecutionException异常显示内容：{}", e);
                }
                break;
            }
            log.info("wait async task to end");
        }
        return Result.ok();
    }
}
